package assist.client.gwt.view;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.web.bindery.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import assist.client.gwt.view.mvp.AppActivityMapper;
import assist.client.gwt.view.mvp.AppPlaceHistoryMapper;
import assist.client.gwt.view.place.SelectGamesPlace;
import assist.domain.model.GamesQuery;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EntryPointMVP implements EntryPoint
{
	private Place defaultPlace = new SelectGamesPlace(new GamesQuery());

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad()
	{
		// Create ClientFactory using deferred binding so we can replace with 
		// different impls in gwt.xml
		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();
	
		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
		activityManager.setDisplay(clientFactory.getAppContainer().getContentArea());

		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper= GWT.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);

		RootLayoutPanel.get().add(clientFactory.getAppContainer());
		// Goes to place represented on URL or default place
		historyHandler.handleCurrentHistory();
	}
}
